<?php get_header()?>
	<div id="left">
		<?php if (!dynamic_sidebar( 'left_sidebar' ) ) { ?>
			
		<?php } ?>
	</div>
	<div id="center">
		<?php
		$current_cat = get_query_var('cat', 0);
		$current_category = get_category($current_cat);
		//pr($current_category);
		if ($current_category->parent==get_category_by_slug('danh-muc')->term_id) {
			$first_posts = get_posts('category='.$current_cat.'&post_type=post&post_status=publish&numberposts=1&meta_key=feature&meta_value=yes');
		} else {
			$first_posts = get_posts('category='.$current_cat.'&post_type=post&post_status=publish&numberposts=1&meta_key=sub-feature&meta_value=yes');
		}
		$first_post = isset($first_posts[0]) ? $first_posts[0] : NULL;
		?>
		<div id="box_center">
			<div class="top">
				<div class="title">
					<?php if ($current_category->parent!=get_category_by_slug('danh-muc')->term_id) {
						print get_category($current_category->parent)->name.' >> ';
					} ?>
					<?php print $current_category->name?></div>
			</div>
			<div class="center">
				
				<?php if (NULL != $first_post) : ?>
					<div id="entry">
						<?php $post = $first_post;
						setup_postdata($post); ?>
						<!--<h2><?php the_title()?></h2>-->
						<p>
							<?php if(has_post_thumbnail()) {
								echo '<a style="float: left; margin-right: 10px" href="'; the_permalink(); echo '">';the_post_thumbnail(array(250, 250)); echo '</a>';
							} else {
								$image = get_first_image(); 
								if ($image):
									echo '<a style="float: left; margin-right: 10px" href="'; the_permalink(); echo'"><img src="'.$image.'" alt="';the_title();echo'" /></a>';
								endif;
							} ?>
							<?php //echo get_the_content();
							$postexcerpt = get_the_content();
							$postexcerpt = apply_filters('the_content', $postexcerpt);
							$postexcerpt = str_replace(']]>', ']]&gt;', $postexcerpt);
							$postexcerpt = strip_tags($postexcerpt);
							$postexcerpt = strip_shortcodes($postexcerpt); ?>
							<?php echo truncate($postexcerpt, 700, ' [...]'); ?>
						</p>
						<div class="clear"></div>
						<div class="more"><a href="<?php the_permalink()?>">>> Xem tiếp</a></div>
					</div>
				<?php endif; ?>
				
				
				<?php if ($current_category->term_id==get_category_by_slug('tuyen-sinh')->term_id || 
							$current_category->term_id==get_category_by_slug('thoi-khoa-bieu')->term_id || 
							$current_category->term_id==get_category_by_slug('lich-thi')->term_id || 
							$current_category->term_id==get_category_by_slug('diem-thi')->term_id || 
							$current_category->term_id==get_category_by_slug('tai-lieu-tham-khao')->term_id) : ?>
					
					<?php
					$sub_categories = get_categories('parent='.$current_cat.'&hide_empty=false');
					//pr($sub_categories);
					if (NULL != $sub_categories) : ?>
						<div id="entry">
							<?php foreach ($sub_categories as $sub_category) : ?>
								<h2><?php print $sub_category->name; //the_title()?></h2>
								<?php $sub_posts = get_posts('category='.$sub_category->term_id.'&orderby=post_date&order=DESC&post_type=post&post_status=publish&numberposts=5');
								if (NULL != $sub_posts) : ?>
									<ul>
										<?php foreach ($sub_posts as $sub_post) : ?>
											<li><a href="<?php print get_permalink($sub_post->ID)?>"><?php print $sub_post->post_title?></a><span class="date-modified"> (<?php print date('d-m-Y', strtotime($sub_post->post_date))?>)</span></li>
										<?php endforeach; ?>
									</ul>
								<?php endif; ?>
								<div class="more"><a href="<?php print get_category_link($sub_category->term_id)?>">>> Xem tất cả</a></div>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>

				<?php else : ?>
				
					<?php
					$sub_categories = get_categories('parent='.$current_cat.'&hide_empty=false');
					//pr($sub_categories);
					if (NULL != $sub_categories) : ?>
						<div id="entry">
							<?php foreach ($sub_categories as $sub_category) :
								$first_posts = get_posts('category='.$sub_category->term_id.'&post_type=post&post_status=publish&numberposts=1&meta_key=sub-feature&meta_value=yes');
								if (NULL == $first_posts) {
									$first_posts = get_posts('category='.$sub_category->term_id.'&orderby=post_date&order=DESC&post_type=post&post_status=publish&numberposts=1');
								}
								$post = isset($first_posts[0]) ? $first_posts[0] : NULL;
								setup_postdata($post); ?>
								<h2><?php print $sub_category->name; //the_title()?></h2>
								<p>
									<?php if(has_post_thumbnail()) {
										echo '<a style="float: left; margin-right: 10px" href="'; the_permalink(); echo '">';the_post_thumbnail(array(250, 250)); echo '</a>';
									} else {
										$image = get_first_image(); 
										if ($image):
											echo '<a style="float: left; margin-right: 10px" href="'; the_permalink(); echo'"><img src="'.$image.'" alt="';the_title();echo'" /></a>';
										endif;
									} ?>
									<?php //echo get_the_content();
									$postexcerpt = get_the_content();
									$postexcerpt = apply_filters('the_content', $postexcerpt);
									$postexcerpt = str_replace(']]>', ']]&gt;', $postexcerpt);
									$postexcerpt = strip_tags($postexcerpt);
									$postexcerpt = strip_shortcodes($postexcerpt); ?>
									<?php echo truncate($postexcerpt, 700, ' [...]'); ?>
								</p>
								<div class="clear"></div>
								<div class="more"><a href="<?php the_permalink()?>">>> Xem tiếp</a></div>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>
					
				<?php endif; ?>
				
				<?php if ($current_category->parent==get_category_by_slug('tuyen-sinh')->term_id || 
							$current_category->parent==get_category_by_slug('thoi-khoa-bieu')->term_id || 
							$current_category->parent==get_category_by_slug('lich-thi')->term_id || 
							$current_category->parent==get_category_by_slug('diem-thi')->term_id || 
							$current_category->parent==get_category_by_slug('tai-lieu-tham-khao')->term_id) : ?>
					<?php 
					$other_posts = get_posts('category='.$current_cat.'&orderby=post_date&order=DESC&post_type=post&post_status=publish&numberposts=-1&exclude='.$first_post->ID);
					//pr($other_posts);
					if (NULL != $other_posts) : ?>
						<div id="others">
							<ul>
								<?php foreach ($other_posts as $p) : ?>
										<li><a href="<?php print get_permalink($p->ID)?>">
											<?php if (empty($p->post_title)) {
												$post_categories = wp_get_post_categories($p->ID);
												$post_category = isset($post_categories[0]) ? $post_categories[0] : 0;
												if (0 != $post_category) {
													print get_category($post_category)->name;
												}
											} else {
												print $p->post_title;
											} ?>
										</a><span class="date-modified"> (<?php print date('d-m-Y', strtotime($p->post_date))?>)</span></li>
								<?php endforeach; ?>
							</ul>
						</div>
					<?php endif; ?>
				<?php endif; ?>
				
			</div>
			<div class="bot"></div>
		</div>
		<?php if (!dynamic_sidebar( 'page_content' ) ) { ?>
			
		<?php } ?>
	</div>
	<div id="right">
		<?php if (!dynamic_sidebar( 'right_sidebar' ) ) { ?>
			
			
		<?php } ?>
	</div>
	<div class="clear"></div>
<?php get_footer()?>
